<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NSpace align="center">
          考核名称:
          <NInput style="width: 200rem" placeholder="请输入考核名称" v-model:value="paginationReactive.appraiseName" clearable />
          <NButton type="primary" style="width: 78rem" ghost
            @click="() => ((paginationReactive.page = 1), getAssessVoteList())">
            查询
          </NButton>
        </NSpace>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :columns="columns" :data="tableData" :pagination="false"
        :loading="tableLoading" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>
</template>
<script setup lang="ts">
import { NSpace, NButton, NDataTable, NPagination, useMessage, NInput } from 'naive-ui'
import { h, reactive, ref, watch, onDeactivated } from 'vue'
import { sendRequest } from '@/utils/api'
import { useRoute } from 'vue-router'
import type { DataTableColumns } from 'naive-ui'
import TableView from '@/components/TableView.vue'
import router from '@/router'
import { useViewerStore } from '@/views/OA/Common/store'
import { aes } from '@/utils/crypto'

const useStore = useViewerStore()
const message = useMessage()
const route = useRoute()

const columns: DataTableColumns = [
  {
    title: '考核名称',
    key: 'appraiseName',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '优秀员工人数',
    key: 'excellentNum',
    width: 80,
    align: useStore.isCenter
  },
  {
    title: '考核人数',
    key: 'appraiseNum',
    width: 120,
    align: useStore.isCenter
  },
  {
    title: '下发时间',
    key: 'sendTime',
    width: 100,
    align: useStore.isCenter
  },
  {
    title: '提交时间',
    key: 'submitTime',
    width: 100,
    align: useStore.isCenter
  },
  {
    title: '截止时间',
    key: 'endTime',
    width: 100,
    align: useStore.isCenter
  },
  {
    title: '状态',
    key: 'status',
    width: 60,
    align: useStore.isCenter,
    render: (row: any) => {
      if (row.status == '0') return '考核中'
      else if (row.status == '1') return '已完成'
      else if (row.status == '2') return '已过期'
      else return '--'
    }
  },
  {
    title: '操作',
    key: 'action',
    align: useStore.isCenter,
    width: 120,
    fixed: 'right',
    render: (row: any) => [
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          style: { display: row.status == '0' ? 'inlne-block' : 'none' },
          onClick: () => {
            router.push({
              name: 'OA/Assess/VoteInfo', params: { appraiseId: row.id },
              query: { scopeId: row.scopeId }
            })
          }
        },
        {
          default: () => '投票'
        }
      )
    ]
  }
]
// 上一页下一页
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  appraiseName: null, //考核名称
  onChange: (page: number) => {
    paginationReactive.page = page
    getAssessVoteList()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    getAssessVoteList()
  }
})
// 获取列表
const tableData = ref([])
const tableLoading = ref(false)
const getAssessVoteList = async () => {
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'assess',
    api: 'assessVoteList',
    override: {
      params: {
        page: paginationReactive.page,
        limit: paginationReactive.pageSize,
        appraiseName: paginationReactive.appraiseName
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  tableData.value = []
  setTimeout(() => {
    tableData.value = res.data.list
    paginationReactive.itemCount = res.data.totalCount
  }, 0)
}

const keyUpDown = (e: any) => e.key == 'Enter' && getAssessVoteList()
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))

watch(
  () => route,
  async () => {
    if (route.path == '/OA/Assess/AssessVoteList') {
      getAssessVoteList()
      window.addEventListener('keyup', keyUpDown, false)  // 回车
    }
  },
  { deep: true, immediate: true }
)
</script>
<style scoped lang="scss">
.table {
  padding: 29rem 30rem 42rem;
}
</style>
  